var Sequelize = require('sequelize');

function Database(base, root, password, driver, port) {
    var sequelize = new Sequelize(base, root, password, {
        dialect: driver, // or 'sqlite', 'postgres', 'mariadb'
        port:    port, //3306 for mysql, 5432 (for postgres)
   //     });
/**********************/
  // custom host; default: localhost
  host: 'localhost',
 

  // custom protocol
  // - default: 'tcp'
  // - added in: v1.5.0
  // - postgres only, useful for heroku
  //protocol: null,
 
  // disable logging; default: console.log
  logging: false,
 
  // max concurrent database requests; default: 50
  maxConcurrentQueries: 100,
 
  // you can also pass any dialect options to the underlying dialect library
  // - default is empty
  // - currently supported: 'mysql', 'mariadb'
  // dialectOptions: {
  //   socketPath: '/tmp/mysql.sock',
  //   supportBigNumbers: true,
  //   bigNumberStrings: true
  // },
 
  // the storage engine for sqlite
  // - default ':memory:'
  //storage: 'path/to/database.sqlite',
 
  // disable inserting undefined values as NULL
  // - default: false
  omitNull: true,
 
  // a flag for using a native library or not.
  // in the case of 'pg' -- set this to true will allow SSL support
  // - default: false
  //native: true,
 
  // Specify options, which are used when sequelize.define is called.
  // The following example:
  //   define: {timestamps: false}
  // is basically the same as:
  //   sequelize.define(name, attributes, { timestamps: false })
  // so defining the timestamps for each model will be not necessary
  // Below you can see the possible keys for settings. All of them are explained on this page
  define: {
    underscored: false
    //freezeTableName: false,
    //syncOnAssociation: true,
    
    //classMethods: {method1: function() {}},
    //instanceMethods: {method2: function() {}},
   // timestamps: true
  },
  charset: 'utf8',
  collate: 'utf8_general_ci',
  // similiar for sync: you can define this to always force sync for models
  sync: { force: true },
 
  // sync after each association (see below). If set to false, you need to sync manually after setting all associations. Default: true
  syncOnAssociation: true,
 
  // use pooling in order to reduce db connection overload and to increase speed
  // currently only for mysql and postgresql (since v1.5.0)
  pool: { maxConnections: 5, maxIdleTime: 30},
 
  // language is used to determine how to translate words into singular or plural form based on the [lingo project](https://github.com/visionmedia/lingo)
  // options are: en [default], es
  language: 'en'
});

    /********/
    var User = sequelize.define('User', {
          username: { type: Sequelize.STRING, primaryKey: true},
          password: { type: Sequelize.STRING } 
        });
    sequelize
    .sync({ force: false })
    .complete(function(err) {
      if (!!err) {
        console.log('An error occurred while creating the table:', err)
      } else {
        console.log('It worked!')
      }
    });
    this.sequelize = sequelize; 
    this.User = User;

}

Database.prototype.auth = function() {
  this.sequelize.authenticate().complete(function(err) {
      if (!!err) {
        console.log('Unable to connect to the database:', err)
      } else {
        console.log('Connection has been established successfully.')
      }
  })
};
  
Database.prototype.sync = function(){
  sequelize
  .sync({ force: false})
  .complete(function(err) {
     if (!!err) {
       console.log('An error occurred while creating the table:', err)
     } else {
       console.log('It worked!')
     }
  });
} 


Database.prototype.create = function(usernameT, passwordT) {
  this.User.create({
    username: usernameT,
    password: passwordT
  })
  .complete(function(err, user) {
    if (!!err) {
      console.log('The instance has not been saved:', err);
    } else {
      console.log('We have a persisted instance now');
    }
  })
}

Database.prototype.find = function(name) {
  this.User.find({ where: { username: name } })
  .complete(function(error, johnDoe) {
    if (!!error) {
      console.log('An error occurred while searching for John Doe:', error);
    } else if (!johnDoe) {
      console.log("No user with the username " + name + " has been found.")
    } else {
      console.log('Hello ' + johnDoe.username + '!');
      console.log('All attributes of john:', johnDoe.values);
    }
  }) 
}

global.Database = Database;
